package com.zxb.core.common.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.*;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Zxb
 */
@Configuration
@EnableOpenApi
@EnableKnife4j
public class SwaggerConfig {

	@Bean
	public Docket mine() {
		return new Docket(DocumentationType.OAS_30)
				.apiInfo(apiInfo())
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.it"))
				.paths(PathSelectors.any())
				.build();
		//.globalRequestParameters(getGlobalRequestParameters());
		//.globalResponses(HttpMethod.GET, getGlobalResponseMessage())
		//.globalResponses(HttpMethod.POST, getGlobalResponseMessage())
		//.globalResponses(HttpMethod.DELETE, getGlobalResponseMessage())
		//.globalResponses(HttpMethod.PUT, getGlobalResponseMessage())
	}

	/**
	 * 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
	 */
	private ApiInfo apiInfo() {
		// 获取工程名称
		String projectName = System.getProperty("user.dir");
		return new ApiInfoBuilder()
				.title(projectName.substring(projectName.lastIndexOf("\\") + 1) + " API接口文档")
				.contact(new Contact("name", "url", "email"))
				.version("2.0")
				.description("description")
				.build();
	}

	/**
	 * 生成全局通用参数
	 *
	 * @return 全局参数
	 */

	@SuppressWarnings("unused")
	private List<RequestParameter> getGlobalRequestParameters() {
		List<RequestParameter> parameters = new ArrayList<>();
		parameters.add(new RequestParameterBuilder()
				.name("access_token")
				.description("令牌")
				.required(true)
				.in(ParameterType.HEADER)
				.build());
		return parameters;
	}

	/**
	 * 生成通用响应信息
	 *
	 * @return 全局相应
	 */
	@SuppressWarnings("unused")
	private List<Response> getGlobalResponseMessage() {
		List<Response> responseList = new ArrayList<>();
		responseList.add(new ResponseBuilder().code("404").description("找不到资源").build());
		return responseList;
	}

}
